//
//  StatusFrame.swift
//  开心微博
//
//  Created by 郑开心 on 15/12/14.
//  Copyright © 2015年 郑开心. All rights reserved.
//

import UIKit

private let StatusCellMargin : CGFloat = 10

class StatusFrame: NSObject {
    
    var status : Status!{
        didSet{
            setUpOriginalViewFrame()
            var toolBarY = CGRectGetMaxY(originalFrame)
            setUpRetweetViewFrame()
            if status.retweeted_status != nil {
                toolBarY = CGRectGetMaxY(retweetViewFrame!)
            }
            let toolBarX : CGFloat = 0
            let toolBarW = Coant.ScreenW
            let toolBarH : CGFloat = 35
            statusToolFrame =  CGRectMake(toolBarX, toolBarY, toolBarW, toolBarH)
            
            cellHeight = CGRectGetMaxY(statusToolFrame)
        }
    }
    
    var originalFrame : CGRect!
    
    var origninalIconFrame : CGRect!
    
    var origninalNameFrame : CGRect!
    
    var origninalVipFrame : CGRect!
    
    var orignianlTimeFrame : CGRect!
    
    var orignianlSourceFrame : CGRect!
    
    var orignianlTextFrame : CGRect!
    
    var orignianlPhotosFrame : CGRect!
    
    var retweetViewFrame : CGRect!
    
    var retweetNameFrame : CGRect!
    var retweetTextFrame : CGRect!
    var retweetPhotosFrame : CGRect!
    
    var statusToolFrame : CGRect!
    
    var cellHeight : CGFloat!

    
    
    func setUpOriginalViewFrame() {
        
        let imageX : CGFloat = StatusCellMargin
        let imageY : CGFloat = StatusCellMargin
        let imageWH : CGFloat = 35
        origninalIconFrame = CGRectMake(imageX, imageY, imageWH, imageWH)
        
        let nameX = CGRectGetMaxX(origninalIconFrame) + StatusCellMargin
        let nameY = imageY
        let nameSize = status.user.name.sizeWithAttributes([NSFontAttributeName:Coant.NameFont])
        
        origninalNameFrame = CGRect(origin: CGPoint(x: nameX, y: nameY), size: nameSize)
        if status.user.vip {
            let vipX = CGRectGetMaxX(origninalNameFrame) + StatusCellMargin
            let vipY = nameY
            let vipWH : CGFloat = 14
            origninalVipFrame = CGRectMake(vipX, vipY, vipWH, vipWH)
        }
        
        let timeX = nameX
        let timeY = CGRectGetMaxY(origninalNameFrame) + StatusCellMargin * 0.5
        let timeSize = status.getcreated_at().sizeWithAttributes([NSFontAttributeName:Coant.NameFont])
        orignianlTimeFrame = CGRect(origin: CGPoint(x: timeX, y: timeY), size: timeSize)
        
    
        let sourceX = CGRectGetMaxX(orignianlTimeFrame) + StatusCellMargin
        let sourceY  = timeY
        let sourceSize = status.source.sizeWithAttributes([NSFontAttributeName:Coant.NameFont])
        orignianlSourceFrame = CGRect(origin: CGPoint(x: sourceX, y: sourceY), size: sourceSize)
        
        
        let textX = imageX
        let textY = CGRectGetMaxY(origninalIconFrame) + StatusCellMargin
        let textW = Coant.ScreenW  - 2 * StatusCellMargin
        let textSize = status.text.boundingRectWithSize(CGSizeMake(textW, CGFloat(MAXFLOAT)), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName:Coant.TextFont]
            , context: nil).size
        orignianlTextFrame = CGRect(origin: CGPoint(x: textX, y: textY), size: textSize)
        //配图
        var originH = CGRectGetMaxY(orignianlTextFrame) + StatusCellMargin
        if status.pic_urls != nil && status.pic_urls?.count > 0 {
            let photosX : CGFloat = imageX
            let photosY : CGFloat = CGRectGetMaxY(orignianlTextFrame) + StatusCellMargin
            let photosSize = photoSizeWithCounts(status.pic_urls!.count)
            orignianlPhotosFrame = CGRect(origin: CGPoint(x: photosX, y: photosY), size: photosSize)
            originH = CGRectGetMaxY(orignianlPhotosFrame) + StatusCellMargin

        }else{
            orignianlPhotosFrame = CGRectMake(0, 0, 0, 0)
        }
        
        let originX : CGFloat = 0
        let originY : CGFloat = 10
        let originW = Coant.ScreenW
        
        originalFrame = CGRectMake(originX, originY, originW, originH)

    }
    
    func photoSizeWithCounts(count: Int) -> CGSize {
    
        let cols = count==4 ? 2 : 3
        let rols = (count - 1) / cols + 1
        var photoWH : CGFloat = Coant.photoWH
        if count % 3 == 0 {
            photoWH = (Coant.ScreenW - 4 * 10) / 3
        }
        let w = CGFloat(cols) * photoWH + CGFloat(cols - 1) * StatusCellMargin
        let h = CGFloat(rols) * photoWH + CGFloat(rols - 1) * StatusCellMargin
        
        return CGSizeMake(w, h)
    }
    
    func setUpRetweetViewFrame() {
        if let retStatus = status.retweeted_status  {
            let nameX = StatusCellMargin
            let nameY = nameX
            // 注意：一定要是转发微博的用户昵称
            let nameSize = retStatus.user.name.sizeWithAttributes([NSFontAttributeName:Coant.NameFont])
            self.retweetNameFrame = CGRect(origin: CGPoint(x: nameX, y: nameY), size: nameSize)
            
            let textX = nameX
            let textY = CGRectGetMaxY(retweetNameFrame!) + StatusCellMargin
            let textW = Coant.ScreenW - 2 * StatusCellMargin
            let textSize = retStatus.text.boundingRectWithSize(CGSizeMake(textW, CGFloat(MAXFLOAT)), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName:Coant.TextFont]
                , context: nil).size
            self.retweetTextFrame = CGRect(origin: CGPoint(x: textX, y: textY), size: textSize)
            
            var retweetH = CGRectGetMaxY(retweetTextFrame) + StatusCellMargin
            
            if retStatus.pic_urls != nil && retStatus.pic_urls?.count > 0 {
                let photosX : CGFloat = StatusCellMargin
                let photosY : CGFloat = CGRectGetMaxY(retweetTextFrame) + StatusCellMargin
                let photosSize = photoSizeWithCounts(retStatus.pic_urls!.count)
                retweetPhotosFrame = CGRect(origin: CGPoint(x: photosX, y: photosY), size: photosSize)
                retweetH = CGRectGetMaxY(retweetPhotosFrame) + StatusCellMargin
                
            }else{
                retweetPhotosFrame = CGRectMake(0, 0, 0, 0)
            }
            let retweetX : CGFloat = 0
            let retweetY = CGRectGetMaxY(originalFrame)
            let retweetW = Coant.ScreenW
            retweetViewFrame = CGRectMake(retweetX, retweetY, retweetW, retweetH)
        }else{
            retweetViewFrame = CGRectMake(0, 0, 0, 0)
            retweetTextFrame = CGRectMake(0, 0, 0, 0)
            retweetNameFrame = CGRectMake(0, 0, 0, 0)
            retweetPhotosFrame = CGRectMake(0, 0, 0, 0)

        }
    }
    

}
